"use strict";

$(function () {
    $(".modal .close").click(function () {
        $(".modal").addClass("hide");
    });
    loadList();
    clickUser();
    //加载列表
    function loadList() {
        $.ajax({
            type: "post",
            url: "/wzmanage/port/queryAll",
            contentType: "application/json;charset=utf-8",
            success: function success(data) {
                console.log(data);
                if (data instanceof Array && data.length > 0) {
                    for (var i = 0, html; i < data.length; i++) {
                        html += "<tr>                           \n                                    <td class=\"name selInfo\">\n                                       <span>" + data[i].name + "</span>\n                                        <input class=\"id\"  type=\"hidden\" value=\"" + data[i].id + "\">\n                                    </td>\n                                \n                                    <td class=\"\">" + data[i].stationId + "</td> \n                                    <td class=\"\">" + ("经度:" + data[i].lng + "&nbsp; 纬度:" + data[i].lat) + "</td> \n                                    <td class=\"updaterole\">\n                                        <button class=\"btn btn-primary btn-large updateRes\" toggle=\"" + data[i].locked + "\">\n                                        " + (data[i].locked ? "显示预报" : "关闭预报") + "</button>\n                                    </td>         \n                                </tr>";
                    }
                    $(".user-list tbody").html(html);
                }
            }
        });
    };
    //选中人员操作
    function clickUser() {
        //点击选中人员所在的行
        $(".user-list tbody").on("click", '.selInfo', function (event) {
            event.stopPropagation();
            if ($(this).parent().hasClass("active")) {
                $(this).parent().removeClass("active");
            } else {
                $(this).parent().addClass("active").siblings(".active").removeClass("active");
            }
        });
    }

    //是否显示预报
    $(".user-list").on("click", "button", function () {
        var locked = $(this).attr("toggle");
        locked = locked == "false" ? "true" : "false";
        console.log(locked);
        var id = $(this).parent().siblings().find(".id").val();
        var obj = { id: id, locked: locked };
        console.log(obj);
        $.ajax({
            type: "post",
            url: "/wzmanage/port/update",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(obj),
            success: function success(data) {
                console.log(data);
                if (data == "Ok") {

                    loadList();
                    $(".new-add").addClass("hide");
                    $(".add-success h3").html("修改成功");
                    $(".add-success").css("display", "block");
                    setTimeout(function () {
                        $(".add-success").fadeOut();
                    }, 500);
                }
            }
        });
    });
    //判断是否为checked
    function ischecked(roleList, lis) {
        for (var j = 0; j < roleList.length; j++) {
            if (roleList[j].id == lis) {
                return 'checked';
            }
        }
        return " ";
    }
    //添加港区
    function addPort(){
        $(".new-add .save").unbind()
        $(".new-add .save").click(function () {
            var port=$(".new-add input[name=lng]").val();
            var station=$(".new-add input[name=lat]").val();
            var obj = $(".user-form").serializeObject();
            var arr=[];
            if (isEmpty() == 1) {
                return;
            }
            if(lnglat(port)==1){
                return
            }else{

            }
            if(lnglat(station)==1){
                return
            }else{
                arr=lnglat(station)
            }
            obj.locked=false;
            obj.lng=arr[0]
            obj.lat=arr[1]
            console.log(obj)
            $.ajax({
                url: "/wzmanage/port/add",
                type: "post",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(obj),
                success: function success(data) {
                    console.log("update" + data);
                    if (data == "Ok") {
                        $('body').css({
                            "overflow-x": "auto",
                            "overflow-y": "auto",
                            'padding-right': '0px'
                        });
                        $(".new-add").addClass("hide");
                        $(".add-success h3").html("添加成功");
                        $(".add-success").css("display", "block");
                        setTimeout(function () {
                            $(".add-success").fadeOut();
                        }, 500);
                        loadList();
                    }
                },
                error: function error(data) {
                    console.log(data);
                }
            });
        });
    }
    //显示增加港区的页面
    $(".add-del-person a").click(function(e){
        e.preventDefault();
        $(".new-add").removeClass("hide");
        addPort()
    })
    function isEmpty() {
        var inps = $(".new-add").find("input");

        for (var i = 0; i < inps.length; i++) {
            if (!inps[i].value) {
                inps[i].focus();
                $(inps[i]).attr("placeholder", "不能为空");
                return 1;
            }
        }

    }
    function lnglat(str) {
        if(str.length<=3){
            alert("输入经纬度数量不够")
            return 1
        }
        var ar=[];
        var arr=str.split(/[,，]/)
        for(var i=0;i<arr.length-1;i++){
            if(!isNaN(arr[i])&&arr[i]){
                if(i%2==0){
                    if(!isNaN(arr[i+1])){
                        if(arr[i]>0&&arr[i]<180&&arr[i+1]<90&&arr[i+1]>0){
                            // ar.push({lng:arr[i],lat:arr[i+1]})
                            ar.push(arr[i])
                            ar.push(arr[i+1])
                        }else{
                            alert("经纬度输入有错误")
                            return 1
                        }
                    }

                }
            }else {
                alert("请在英文状态下输入经纬度");
                return 1;
            }
        }
        return ar
    }
    //插件
    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };
});